$(function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
    //1 绑定添加按钮
    $('.add_btn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加部门页面',
            area:['600px','400px'],
            content:'initEditSysDep.do?opera=add',
            maxmin: true,
            shadeClose: true
        });
    });
    
    //4 绑定查询按钮
    $('.sel_btn').click(function(){
    	paging(1);
    });
    
    //5 绑定重置按钮
    $('.reset_btn').click(function(){
    	$("#depName").val('');
    	$("#depName").focus();
    });
    //6.绑定设置权限按钮
    $('.pri_btn').click(function(){
    	pri_func(this);
    })
    //7.删除选定的，多项删除
    $('.del_select_btn').click(function(){
    	del_select_func(this);
    });
    
    //ajax初始化数据
    paging(1);
});
edit_func=function(obj){
    var depId=$(obj).parent().parent().attr('depId');
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改子部门页面',
        area:['600px','400px'],
        content:'initEditSysDep.do?opera=edit&depId='+depId,
        maxmin: true,
        shadeClose: true
    });
}
delete_func=function(obj){
    var depId=$(obj).parent().parent().attr('depId');
    if(confirm('确定删除该 子部门?')){
        $.ajax({
            url:"deleteDepByDepId.do",
            type:"post",
            dataType: 'json',
            data:{
                "depId":depId
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('删除成功', {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

function del_select_func(obj){
	var checkedDel = "";
	 var arr=document.getElementsByName("checkradio");
	 	
	 	for(i=0;i<arr.length;i++){
			if(arr[i].checked){
			var a = arr[i].value;
			checkedDel+=a+",";
			}
		}
		if(checkedDel==""){
			layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
		}else{
			if(confirm('确定删除所有选中的部门?')){
			$.ajax({
            url:"deleteSelectSysDep.do",
            type:"post",
            dataType: 'json',
            data:{
                "checkedDel":checkedDel
            },
            beforeSend:function(){
                
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('删除成功', {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
			}
		}
}

function paging(pageNo) {
	var pageSize = $("#pageSize  option:selected").text();
    var depName = $("#depName").val();
    var depId=$("#depId").val();
    $.ajax({
        url: "pagingDepChildList.do",
        type: "post",
        dataType: 'json',
        data: {
        	"iPageSize": pageSize,
            "iPageNo": pageNo,
            "depName": depName,
            "depId":depId
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
            if (jsons != null) {
                $("#pageTable").html("");
                var pageList = jsons.pageData.pageList;
                var companyList=jsons.companyList;
                var cmpNameFrom="";
                var isChildDep="";
                for (var i = 0; i < pageList.length; i++) {
                	for(var j=0;j<companyList.length;j++){
                		if(pageList[i].cmpId==companyList[j].cmpId){
                    		cmpNameFrom='<td title=' +(companyList[j].cmpName == null ? '' : companyList[j].cmpName) + '>' + (companyList[j].cmpName == null ? '' : companyList[j].cmpName)+ '</td>';
                   	 	}
                 	}
                 	if(pageList[i].depPid==0){
                 		isChildDep='<i class="Pri content_current depChild_btn" title="查看子部门"></i>';
                 	}
                    var partsForm =
                        '<tr depId="'+pageList[i].depId +'">' +
                        '<td class="checkNot"><input type="checkbox" value="' + pageList[i].depId + '" class="checkinput" name="checkradio"/></td>' +
                        //'<td title=' + (pageList[i].depId == null ? '' : pageList[i].depId) + '>' + (pageList[i].depId == null ? '' : pageList[i].depId) + '</td>' +
                        '<td title=' + (pageList[i].depCode == null ? '' : pageList[i].depCode) + '>' + (pageList[i].depCode == null ? '' : pageList[i].depCode) + '</td>' +
                        cmpNameFrom +
                        '<td title=' + (pageList[i].depName == null ? '' : pageList[i].depName) + '>' + (pageList[i].depName == null ? '' : pageList[i].depName) + '</td>' +
                        '<td title=' + (pageList[i].state == "9ba" ? "启用" : "关闭") + '>' + (pageList[i].state == "9ba" ? "启用" : "关闭") + '</td>' +
                        '<td title=' + (pageList[i].remark == null ? '' : pageList[i].remark) + '>' + (pageList[i].remark == null ? '' : pageList[i].remark) + '</td>' +
                        '<td><i class="Edit content_current edit_btn" title="编辑"></i>' +
                        '<i class="Del content_current delete_btn" title="删除"></i>' +
                        '</td>' +
                        '</tr>';
                    $(partsForm).appendTo("#pageTable");
                }
                //3 绑定编辑按钮
                $('.edit_btn').click(function(){
                    edit_func(this);
                });
                $('.delete_btn').click(function(){
                    delete_func(this);
                });
                $('.depChild_btn').click(function(){
                    depChild_btn(this);
                });
                //处理footer 分页
                var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);
                
                //定位焦点
                $("#depName").focus();
            }
        }
    });
}

//全选框选择
$(".CheckAll").change(function (){
	$(":checkbox[name='PageItem']").attr("checked",this.checked);
});


//----------------------------------------------------------------------------------------
/**
 * 删除ci类型绑定的关系
 */
function deleteCiRelation(){
	if($("input[name='relationPageItem']:checked").length==0){
		layer.msg("请选择要删除的关系类型",{time:1500});
	}
	var ciRelationIds = "";
	$("input[name='relationPageItem']:checked").each(function(){
		ciRelationIds = ciRelationIds+$(this).val()+",";
	});
	ciRelationIds = ciRelationIds.substring(0,ciRelationIds.length-1);
	$.ajax({
		url:"deleteCiTypeRelation.do",
		type:"post",
		dataType: 'json',
		data:{
			"ciTypeId":$("#selectCiTypeId").val(),
			"ciTypeRelationIds":ciRelationIds
		},
		beforeSend:function(){
			$("#press_btn1").attr("disabled","disabled");
		},
		error:function(data){  
			layer.msg('出错了！！',{time:2000}); 
		},
		success:function(result){
			if(result.flag){
				layer.msg('删除成功', {time:1300});
				parent.pgReljump(15,1,'ci_relation_id desc');
				var index = parent.layer.getFrameIndex(window.name);
				setTimeout('parent.layer.close('+index+')',1310);
			}else{
				layer.msg('删除失败！', {time:1000});
				$("#press_btn1").removeAttr("disabled");
			}
		}
	});
}